package cn.kgc.ywz.smbms.web.service.impl;

import cn.kgc.ywz.smbms.web.entity.Role;
import cn.kgc.ywz.smbms.web.entity.User;
import cn.kgc.ywz.smbms.web.mapper.RoleMapper;
import cn.kgc.ywz.smbms.web.mapper.UserMapper;
import cn.kgc.ywz.smbms.web.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 登录
     */
    @Override
    public User findUser(String userCode, String userPassword) {
        return this.userMapper.login(userCode, userPassword);
    }

    /**
     * 查询所有or条件
     */
    @Override
    public PageInfo<User> findAllOrCondition(User condition, Integer pageNum, Integer pageSize) {
        if (pageNum == null) pageNum = 1;
        pageSize = 3;
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = this.userMapper.selectAllOrCondition(condition);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    /**
     * 添加
     */
    @Override
    public int add(User user) {
        return this.userMapper.insert(user);
    }

    /**
     * 删除
     */
    @Override
    public int remove(long userId) {
        return this.userMapper.delete(userId);
    }

    /**
     * 修改
     */
    @Override
    public int modif(User user) {
        return this.userMapper.update(user);
    }

    /**
     * 查看旧密码是否正确
     */
    @Override
    public String findOldPassword(long userId) {
        return this.userMapper.selectOldPassword(userId);
    }

    /**
     * 修改密码
     */
    @Override
    public int updatePassword(long userId, String password) {
        return this.userMapper.updatePassword(userId,password);
    }

    /**
     * 查询编码
     */
    @Override
    public Integer findByUserCode(String userCode) {
        return this.userMapper.selectByUserCode(userCode);
    }

    /**
     * 查询详情
     */
    @Override
    public User findById(Long userId) {
        return this.userMapper.selectById(userId);
    }

    /**
     * 根据角色查询name
     */
    @Override
    public List<User> findByRoleId(Long roleId) {
        return this.userMapper.selectByRoleId(roleId);
    }
}
